package secret

import (
	"fmt"

	"gitee.com/micro-plat/sas/sas/modules/const/conf"
	"github.com/micro-plat/lib4go/types"
	cache "github.com/patrickmn/go-cache"
)

var localCache *cache.Cache

func init() {
	localCache = cache.New(conf.DefaultExpiration, conf.CleanupInterval)
}

func GetSecretCache(euid string) (data types.XMap, err error) {

	//从缓存中获取
	cacheInfo, exist := localCache.Get(euid)
	if exist {
		data = cacheInfo.(types.XMap)
		return
	}
	//获取数据
	db := NewDBSecret()
	data, err = db.GetPermission(euid)
	if err != nil {
		err = fmt.Errorf("获取[%s]密钥信息失败:%+v", euid, err)
		return
	}

	if data.IsEmpty() {
		err = fmt.Errorf("未配置[%s]密钥信息", euid)
		return
	}

	//存入缓存
	localCache.Set(euid, data, cache.DefaultExpiration)
	return
}

func deleteCache(euid string) {
	localCache.Delete(euid)
}
